package string_helper;

/**
 * Author: Daniel
 * Date: 19.09.13
 */

public class StringHelper {
    /**
     * Remove all charracter 'A' in first 2 positions of the given string.
     *
     * @param str given string
     * @return result of truncate. Excample  "ABCD" => "BCD", "AACD" => "CD"
     *         "AAAA" => "AA"
     */
    public String truncateInFirst2Positions(String str) {
        if (str == null) return null;
        if (str.length() <= 2)
            return str.replace("A", "");

        String first2Chars = str.substring(0, 2);
        String stringMinusFirst2Chars = str.substring(2);

        return first2Chars.replaceAll("A", "") + stringMinusFirst2Chars;
    }

    public boolean areFirstAndLastTwoCharactersTheSame(String str) {
        if (str == null) return false;

        if (str.length() <= 1) return false;
        if (str.length() == 2) return true;

        String first2Chars = str.substring(0, 2);
        String last2Chars = str.substring(str.length() - 2);

        return first2Chars.equals(last2Chars);
    }
}
